﻿using System.ComponentModel;
using System.Globalization;
using System.Reflection;
using System.Security.Cryptography;
using System.Text;
using System.Text.Json;
using System.Text.RegularExpressions;
using System.Xml;

namespace LF.Common;
public static class Extend
{
    private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1,0,0,0,DateTimeKind.Utc);

    public static long ToUtc(this DateTime time)
    {
        return (long)(time.ToUniversalTime() - Jan1st1970).TotalMilliseconds;
    }

    /// <summary>
    /// JSON 序列化为
    /// </summary>
    /// <typeparam name="T">被序列化对象</typeparam>
    /// <param name="data">JSON 字符串</param>
    /// <returns></returns>
    public static T? JsonConvert<T>(this string data)
    {
        if (string.IsNullOrEmpty(data)) return default;

        return JsonSerializer.Deserialize<T>(data, new JsonSerializerOptions { PropertyNameCaseInsensitive = true });
    }

    public static string ToMd5Lower(this string str)
    {
        string pwd = "";
        if (!string.IsNullOrEmpty(str))
        {
            MD5 md5 = MD5.Create(); 
            // 加密后是一个字节类型的数组，这里要注意编码UTF8/Unicode等的选择　
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(str));
            // 通过使用循环，将字节类型的数组转换为字符串，此字符串是常规字符格式化所得
            for (int i = 0; i < s.Length; i++)
            {
                // 将得到的字符串使用十六进制类型格式。格式后的字符是小写的字母，如果使用大写（X）则格式后的字符是大写字符 
                pwd = pwd + s[i].ToString("X");
            }
        }
        return pwd;
    }

    public static string ToPath(this string str)
    {
        return Path.Combine(AppConfig.BasePath, str);
    }

    public static bool IsEmpty(this string str)
    {
        return string.IsNullOrWhiteSpace(str);
    }
    /// <summary>
    /// 是否是中国手机号
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static bool IsMobile(this string str)
    {
        return new Regex(@"^(13[0-9]|14[01456879]|15[0-35-9]|16[2567]|17[0-8]|18[0-9]|19[0-35-9])\d{8}$").IsMatch(str);
    }
    /// <summary>
    /// 快速MD5加密
    /// </summary>
    /// <param name="str"></param>
    /// <returns>小写MD5值</returns>
    public static string ToFastMd5(this string str)
    {
        var inputBytes = Encoding.UTF8.GetBytes(str);
        var hashBytes = MD5.HashData(inputBytes);
        return Convert.ToHexString(hashBytes);
    }

    public static string Description<T>(this T e) where T : IConvertible
    {
        string desc = string.Empty;

        if (e is Enum)
        {
            Type type = e.GetType();
            Array values = Enum.GetValues(type);

            foreach (int val in values)
            {
                if (val == e.ToInt32(CultureInfo.InvariantCulture))
                {
                    MemberInfo[] memInfo = type.GetMember(type.GetEnumName(val));
                    object[] descriptionAttributes = memInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
                    if (descriptionAttributes.Length > 0)
                    {
                        // we're only getting the first description we find
                        // others will be ignored
                        desc = ((DescriptionAttribute)descriptionAttributes[0]).Description;
                    }

                    break;
                }
            }
        }
        return desc;
    }

}

